package doct.mobi.album.myrxjava;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

/**
 * Created by huangli on 17/12/26.
 */

public class FlowAbleResponsiveActivity extends Activity{

    public static final String TAG = "FlowAbleResponsive";

    public static Subscription subscriber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flowable);
        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"点击 btn_start");
                RxManager.flowableEmitterResponsive(new Subscriber<Integer>() {
                    @Override
                    public void onSubscribe(Subscription s) {
                        subscriber = s;
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i(TAG,"onNext : "+integer);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.i(TAG,"onError : "+t.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG,"onComplete");
                    }
                });
            }
        });
        findViewById(R.id.btn_req).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"点击 btn_req");
                if (subscriber != null){
                    Log.i(TAG,"request");
                    subscriber.request(96); //这里如果设置小于96是不会再次请求的（具体可以查看源码）
                }
            }
        });
    }
}
